// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.Data;

namespace Northwind.DomainModel.Data
{

    /// <summary />
    public partial class EmployeeMapper
    {

        /// <summary />
        static EmployeeMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainEntityKeyedCollection<Employee> _FindAll(DataMapperCollection dataMappers)
        {
            IDbCommand[] commands=new IDbCommand[1];

            commands[0]=Connection.CreateCommand();
            commands[0].CommandType=CommandType.Text;
            commands[0].CommandText=Resources.Sql.EmployeeSelectAll;

            return Load(commands, dataMappers);
        }

        private Employee.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Employee.DataTransfer data=new Employee.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Employee.DataTransfer FillData(Employee.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("EmployeeID");
            if (!record.IsDBNull(ord))
                data.EmployeeID=record.GetInt32(ord);

            ord=record.GetOrdinal("LastName");
            if (!record.IsDBNull(ord))
                data.LastName=record.GetString(ord);

            ord=record.GetOrdinal("FirstName");
            if (!record.IsDBNull(ord))
                data.FirstName=record.GetString(ord);

            ord=record.GetOrdinal("Title");
            if (!record.IsDBNull(ord))
                data.Title=record.GetString(ord);

            ord=record.GetOrdinal("TitleOfCourtesy");
            if (!record.IsDBNull(ord))
                data.TitleOfCourtesy=record.GetString(ord);

            ord=record.GetOrdinal("BirthDate");
            if (!record.IsDBNull(ord))
                data.BirthDate=record.GetDateTime(ord);

            ord=record.GetOrdinal("HireDate");
            if (!record.IsDBNull(ord))
                data.HireDate=record.GetDateTime(ord);

            ord=record.GetOrdinal("Address");
            if (!record.IsDBNull(ord))
                data.Address=record.GetString(ord);

            ord=record.GetOrdinal("City");
            if (!record.IsDBNull(ord))
                data.City=record.GetString(ord);

            ord=record.GetOrdinal("Region");
            if (!record.IsDBNull(ord))
                data.Region=record.GetString(ord);

            ord=record.GetOrdinal("PostalCode");
            if (!record.IsDBNull(ord))
                data.PostalCode=record.GetString(ord);

            ord=record.GetOrdinal("Country");
            if (!record.IsDBNull(ord))
                data.Country=record.GetString(ord);

            ord=record.GetOrdinal("HomePhone");
            if (!record.IsDBNull(ord))
                data.HomePhone=record.GetString(ord);

            ord=record.GetOrdinal("Extension");
            if (!record.IsDBNull(ord))
                data.Extension=record.GetString(ord);

            ord=record.GetOrdinal("Notes");
            if (!record.IsDBNull(ord))
                data.Notes=record.GetString(ord);

            return data;
        }

        /// <summary />
        private static IDbCommand _CreateSelectCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Employee.PrimaryKey key)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.EmployeeSelect,
                parameterBuilder.GetMarker("EmployeeID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("EmployeeID", 1);
            p.DbType=DbType.Int32;
            p.Value=key.Value;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateDeleteCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Employee.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.EmployeeDelete,
                parameterBuilder.GetMarker("EmployeeID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("EmployeeID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.EmployeeID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateInsertCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Employee.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.EmployeeInsert,
                parameterBuilder.GetMarker("LastName", 1),
                parameterBuilder.GetMarker("FirstName", 2),
                parameterBuilder.GetMarker("Title", 3),
                parameterBuilder.GetMarker("TitleOfCourtesy", 4),
                parameterBuilder.GetMarker("BirthDate", 5),
                parameterBuilder.GetMarker("HireDate", 6),
                parameterBuilder.GetMarker("Address", 7),
                parameterBuilder.GetMarker("City", 8),
                parameterBuilder.GetMarker("Region", 9),
                parameterBuilder.GetMarker("PostalCode", 10),
                parameterBuilder.GetMarker("Country", 11),
                parameterBuilder.GetMarker("HomePhone", 12),
                parameterBuilder.GetMarker("Extension", 13),
                parameterBuilder.GetMarker("Notes", 14)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("LastName", 1);
            p.DbType=DbType.String;
            p.Value=data.LastName;
            p.Size=20;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("FirstName", 2);
            p.DbType=DbType.String;
            p.Value=data.FirstName;
            p.Size=10;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Title", 3);
            p.DbType=DbType.String;
            p.Value=(object)data.Title ?? (object)DBNull.Value;
            p.Size=30;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("TitleOfCourtesy", 4);
            p.DbType=DbType.String;
            p.Value=(object)data.TitleOfCourtesy ?? (object)DBNull.Value;
            p.Size=25;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("BirthDate", 5);
            p.DbType=DbType.Date;
            p.Value=data.BirthDate;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("HireDate", 6);
            p.DbType=DbType.Date;
            p.Value=data.HireDate;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Address", 7);
            p.DbType=DbType.String;
            p.Value=(object)data.Address ?? (object)DBNull.Value;
            p.Size=60;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("City", 8);
            p.DbType=DbType.String;
            p.Value=(object)data.City ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Region", 9);
            p.DbType=DbType.String;
            p.Value=(object)data.Region ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("PostalCode", 10);
            p.DbType=DbType.String;
            p.Value=(object)data.PostalCode ?? (object)DBNull.Value;
            p.Size=10;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Country", 11);
            p.DbType=DbType.String;
            p.Value=(object)data.Country ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("HomePhone", 12);
            p.DbType=DbType.String;
            p.Value=(object)data.HomePhone ?? (object)DBNull.Value;
            p.Size=24;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Extension", 13);
            p.DbType=DbType.String;
            p.Value=(object)data.Extension ?? (object)DBNull.Value;
            p.Size=4;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Notes", 14);
            p.DbType=DbType.String;
            p.Value=(object)data.Notes ?? (object)DBNull.Value;
            p.Size=8192;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateUpdateCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Employee.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.EmployeeUpdate,
                parameterBuilder.GetMarker("LastName", 1),
                parameterBuilder.GetMarker("FirstName", 2),
                parameterBuilder.GetMarker("Title", 3),
                parameterBuilder.GetMarker("TitleOfCourtesy", 4),
                parameterBuilder.GetMarker("BirthDate", 5),
                parameterBuilder.GetMarker("HireDate", 6),
                parameterBuilder.GetMarker("Address", 7),
                parameterBuilder.GetMarker("City", 8),
                parameterBuilder.GetMarker("Region", 9),
                parameterBuilder.GetMarker("PostalCode", 10),
                parameterBuilder.GetMarker("Country", 11),
                parameterBuilder.GetMarker("HomePhone", 12),
                parameterBuilder.GetMarker("Extension", 13),
                parameterBuilder.GetMarker("Notes", 14),
                parameterBuilder.GetMarker("EmployeeID", 15)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("LastName", 1);
            p.DbType=DbType.String;
            p.Value=data.LastName;
            p.Size=20;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("FirstName", 2);
            p.DbType=DbType.String;
            p.Value=data.FirstName;
            p.Size=10;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Title", 3);
            p.DbType=DbType.String;
            p.Value=(object)data.Title ?? (object)DBNull.Value;
            p.Size=30;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("TitleOfCourtesy", 4);
            p.DbType=DbType.String;
            p.Value=(object)data.TitleOfCourtesy ?? (object)DBNull.Value;
            p.Size=25;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("BirthDate", 5);
            p.DbType=DbType.Date;
            p.Value=data.BirthDate;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("HireDate", 6);
            p.DbType=DbType.Date;
            p.Value=data.HireDate;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Address", 7);
            p.DbType=DbType.String;
            p.Value=(object)data.Address ?? (object)DBNull.Value;
            p.Size=60;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("City", 8);
            p.DbType=DbType.String;
            p.Value=(object)data.City ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Region", 9);
            p.DbType=DbType.String;
            p.Value=(object)data.Region ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("PostalCode", 10);
            p.DbType=DbType.String;
            p.Value=(object)data.PostalCode ?? (object)DBNull.Value;
            p.Size=10;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Country", 11);
            p.DbType=DbType.String;
            p.Value=(object)data.Country ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("HomePhone", 12);
            p.DbType=DbType.String;
            p.Value=(object)data.HomePhone ?? (object)DBNull.Value;
            p.Size=24;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Extension", 13);
            p.DbType=DbType.String;
            p.Value=(object)data.Extension ?? (object)DBNull.Value;
            p.Size=4;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Notes", 14);
            p.DbType=DbType.String;
            p.Value=(object)data.Notes ?? (object)DBNull.Value;
            p.Size=8192;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("EmployeeID", 15);
            p.DbType=DbType.Int32;
            p.Value=data.EmployeeID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private ParameterBuilder _ParameterBuilder;

    }

}
